﻿Imports System.Data.SqlClient
Imports DMC.Land.User
Imports System.Xml
Public Class CtrDanhSachHoSo
    Private strNamIn As Int16 = 0
    Private strConnection As String = ""
    Public User As New clsUser
    Private strUsername As String = ""
    Private strDVHC As String
    Private strDonViHanhChinh As String = ""
    Private strMaQuyen As String = ""
    Dim conn = New SqlConnection
    Private strsoMaVach As String = ""
    Private strTuNgay As String = ""
    Private strNgay As String = ""
    Private strDenNgay As String = ""
    Private strThang As String = ""
    Private strNam As String = ""
    Private shortAction As String = ""
    Private strError As String = ""
    Private strMa As String = ""

    Private Sub TabControl1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseClick
        If TabPage1.Focus Then
            Me.somavach.Focus()
            Me.somavach.SelectAll()
           
        End If
       

    End Sub
    Public Property Connection() As String
        Get
            Return strConnection
        End Get
        Set(ByVal value As String)
            strConnection = value
        End Set
    End Property
    Public Property MaQuyen() As String
        Get
            Return strMaQuyen
        End Get
        Set(ByVal value As String)
            strMaQuyen = value
        End Set
    End Property
    Public Property DVHC() As String
        Get
            Return strDVHC
        End Get
        Set(ByVal value As String)
            strDVHC = value
        End Set
    End Property
    Public Property UserName() As String
        Get
            Return strUsername
        End Get
        Set(ByVal value As String)
            strUsername = value
        End Set
    End Property
    Public Sub ctrLoad_Tonghop() '(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            conn.ConnectionString = strConnection
            Try
                conn.Open()
            Catch ex As Exception
                MessageBox.Show("Lỗi kết nối" & ex.Message)
            End Try    

        Catch ex As Exception
            MsgBox("Connection failen !")

            Me.Dispose()
        End Try
    End Sub
    Private Sub somavach_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles somavach.TextChanged
        somavach.SelectAll()
        If somavach.SelectedText <> "" Then
            somavach.Text = somavach.Text.Replace(somavach.SelectedText, somavach.Text.ToString)
        Else
            somavach.Text = ""
        End If
        
            LoadHoSo()
      
    End Sub
    Public Sub LoadHoSo()
        Dim cls As New Clslaphosophaply
        Dim dtHoSo As New DataTable
        cls.Connection = strConnection
        strNgay = Me.DateTimePicker1.Value.Day
        strThang = Me.DateTimePicker1.Value.Month
        strNam = Me.DateTimePicker1.Value.Year
        strNgay = strThang + "/" + strNgay + "/" + strNam

        strDonViHanhChinh = strDVHC
        shortAction = "0"
        Dim parasHoSoSoDKBD() As String = {"@GiaTriMaVach", "@Ngay", "@action", "@MaHoSo"}
        Dim ValuesHoSo() As String = {Me.somavach.Text.ToString(), strNgay, shortAction, ""}
        dtHoSo = cls.GetData_SP("SelectQuetTimThongTinByMaVach", parasHoSoSoDKBD, ValuesHoSo)

        Me.CtrlGridView1.DataSource = dtHoSo
        CtrlGridView1.Columns("STT").HeaderText = "Số TT"
        CtrlGridView1.Columns("id").Visible = False
        CtrlGridView1.Columns("MaHoSoCapGCN").Visible = False
        CtrlGridView1.Columns("GiaTriMaVach").Visible = False
        CtrlGridView1.Columns("TrangThaiHoSoCapGCN").Visible = False
        CtrlGridView1.Columns("NgayLap").Visible = False
        CtrlGridView1.Columns("ChuSuDung").HeaderText = "Họ và tên chủ sử dụng"
        CtrlGridView1.Columns("DiaChi").HeaderText = "Địa Chỉ"
        CtrlGridView1.Columns("TongDT").HeaderText = "Tổng DT"
        CtrlGridView1.Columns("DatO").HeaderText = "Đất ở"
        CtrlGridView1.Columns("DTRiengDatO").HeaderText = "DT riêng đất ở"
        CtrlGridView1.Columns("DTChungDatO").HeaderText = "DT chung đất ở"
        CtrlGridView1.Columns("DatAo").HeaderText = "Đất ao, vườn, nông nghiệp"
        CtrlGridView1.Columns("SoThua").HeaderText = "Số thửa"
        CtrlGridView1.Columns("SoTo").HeaderText = "Số tờ"
        CtrlGridView1.Columns("DTXD").HeaderText = "Diện tích xây dựng"
        CtrlGridView1.Columns("LoaiNha").HeaderText = "Loại nhà"
        CtrlGridView1.Columns("SoPhatHanh").HeaderText = "Số phát hành, ghi chú"
        CtrlGridView1.Columns("SoVaoSoCapGCN").HeaderText = "Số vào sổ"
        CtrlGridView1.Columns("TongDTD").Visible = False
        CtrlGridView1.Columns("TongRiengO").Visible = False
        CtrlGridView1.Columns("TongChungO").Visible = False
        CtrlGridView1.Columns("TongDTO").Visible = False
        CtrlGridView1.Columns("TongAo").Visible = False
        CtrlGridView1.Columns("Thue").Visible = False
        shortAction = ""
        ' CtrlGridView1.Columns("TongDT").DefaultCellStyle.Format = "dd / MM / yyyy"
    End Sub

    Private Sub CtrlGridView1_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles CtrlGridView1.CellMouseClick
        If e.RowIndex >= 0 Then
            Try
                With Me

                    strMa = CtrlGridView1.Item("MaHoSoCapGCN", e.RowIndex).Value.ToString()

                End With
            Catch ex As Exception
                MsgBox(" Bảng mã tham số mặc định " & vbNewLine & " Lỗi: " & vbNewLine & ex.Message, MsgBoxStyle.Information, "DMCLand")
            End Try
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Nếu tồn tại mã cần xóa
        If strMa <> "" Then
            If MsgBox("Bạn chắc chắn muốn xóa không?", MsgBoxStyle.YesNo, "DMCLand!") = MsgBoxResult.Yes Then
                Try
                    Dim cls As New Clslaphosophaply
                    With Me
                        shortAction = "1"
                        cls.Connection = strConnection
                        cls.MaHoSo = strMa
                        cls.Action = shortAction
                        Dim i As Integer
                        cls.Delete(i)
                    End With
                    strError = cls.Err
                Catch ex As Exception
                    strError = ex.Message
                End Try
                If (strError = "") Or (strError = "OK") Then
                    MsgBox(" Bạn đã xóa thành công!", MsgBoxStyle.Information, "DMCLand")
                Else
                    MsgBox(" chưa xóa thành công!", MsgBoxStyle.Critical, "DMCLand")
                End If
            End If
        Else
            MsgBox(" Bạn chưa chọn tham số cần xóa!", MsgBoxStyle.Information, "DMCLand")
        End If

        shortAction = "0"
        somavach.Text = ""
        LoadHoSo()
        strMa = ""
        strError = ""
    End Sub

    

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        thongke()

    End Sub

    Public Sub thongke()
        Dim cls As New Clslaphosophaply
        Dim dtHoSo As New DataTable
        cls.Connection = strConnection

        strNgay = Me.tungay.Value.Day
        strThang = Me.tungay.Value.Month
        strNam = Me.tungay.Value.Year
        strTuNgay = strThang + "/" + strNgay + "/" + strNam

        strNgay = "'"
        strThang = ""
        strNam = ""
        strNgay = Me.denngay.Value.Day
        strThang = Me.denngay.Value.Month
        strNam = Me.denngay.Value.Year
        strDenNgay = strThang + "/" + strNgay + "/" + strNam

        strDonViHanhChinh = strDVHC

        If check() = True Then
            Dim parasHoSoSoDKBD() As String = {"@GiaTriMaVach", "@TuNgay", "@DenNgay", "@CapMoi"}
            Dim ValuesHoSo() As String = {Me.txtmavach.Text.ToString(), strTuNgay, strDenNgay, "1"}
            dtHoSo = cls.GetData_SP("ThongKeHoSoDuTinhPhapLy", parasHoSoSoDKBD, ValuesHoSo)

        Else
            Dim parasHoSoSoDKBD() As String = {"@GiaTriMaVach", "@TuNgay", "@DenNgay", "@CapMoi"}
            Dim ValuesHoSo() As String = {Me.txtmavach.Text.ToString(), strTuNgay, strDenNgay, "0"}
            dtHoSo = cls.GetData_SP("ThongKeHoSoDuTinhPhapLy", parasHoSoSoDKBD, ValuesHoSo)

        End If
       
        
        Me.CtrlGridView4.DataSource = dtHoSo
        CtrlGridView4.Columns("STT").HeaderText = "Số TT"
        CtrlGridView4.Columns("id").Visible = False
        CtrlGridView4.Columns("MaHoSoCapGCN").Visible = False
        CtrlGridView4.Columns("GiaTriMaVach").Visible = False
        CtrlGridView4.Columns("TrangThaiHoSoCapGCN").Visible = False
        CtrlGridView4.Columns("DiaChi").HeaderText = "Địa Chỉ"
        CtrlGridView4.Columns("TongDT").HeaderText = "Tổng DT"
        CtrlGridView4.Columns("DatO").HeaderText = "Đất ở"
        CtrlGridView4.Columns("DTRiengDatO").HeaderText = "DT riêng đất ở"
        CtrlGridView4.Columns("DTChungDatO").HeaderText = "DT chung đất ở"
        CtrlGridView4.Columns("DatAo").HeaderText = "Đất ao, vườn, nông nghiệp"
        CtrlGridView4.Columns("DTXD").HeaderText = "Diện tích xây dựng"
        CtrlGridView4.Columns("LoaiNha").HeaderText = "Loại nhà"
        CtrlGridView4.Columns("SoThua").HeaderText = "Số thửa"
        CtrlGridView4.Columns("SoTo").HeaderText = "Số tờ"
        CtrlGridView4.Columns("NgayLap").Visible = False
        CtrlGridView4.Columns("ChuSuDung").HeaderText = "Họ và tên chủ sử dụng"
        CtrlGridView4.Columns("SoPhatHanh").HeaderText = "Số phát hành, ghi chú"
        CtrlGridView4.Columns("SoVaoSoCapGCN").HeaderText = "Số vào sổ"
        CtrlGridView4.Columns("TongDTD").Visible = False
        CtrlGridView4.Columns("TongRiengO").Visible = False
        CtrlGridView4.Columns("TongChungO").Visible = False
        CtrlGridView4.Columns("TongDTO").Visible = False
        CtrlGridView4.Columns("TongAo").Visible = False
        CtrlGridView4.Columns("Thue").Visible = False
        shortAction = ""
    End Sub


    Private Sub rdbiendong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbiendong.Click
        rdbiendong.Checked = True
        rdcapmoi.Checked = False
    End Sub

    Private Sub rdcapmoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdcapmoi.Click
        rdcapmoi.Checked = True
        rdbiendong.Checked = False
    End Sub
    Public Function check() As Boolean
        Dim kt As Boolean = False
        If rdcapmoi.Checked = True Then
            kt = True
        Else
            kt = False
        End If

        Return kt
    End Function


    Public Function DonViHanhChinh() As String()
        Dim cls As New Clslaphosophaply
        cls.Connection = strConnection
        cls.MaDonViHanhChinh = strDVHC
        Dim dtDVHC As New DataTable
        dtDVHC = cls.SelectDVHC
        Dim arrMangGiaTriDVHC(3) As String
        If dtDVHC.Rows.Count > 0 Then
            arrMangGiaTriDVHC(0) = dtDVHC.Rows(0).Item("TenTinh").ToString.ToUpper
            arrMangGiaTriDVHC(1) = dtDVHC.Rows(0).Item("TenHuyen").ToString.ToUpper
            arrMangGiaTriDVHC(2) = dtDVHC.Rows(0).Item("Ten").ToString.ToUpper
        End If
        Return arrMangGiaTriDVHC
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cls As New Clslaphosophaply
        cls.Connection = strConnection
        If (strDVHC = 0) Then
            MessageBox.Show("Chọn đơn vị hành chính", "DMCLand", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End If

        Dim dtHoSo As New DataTable
        cls.Connection = strConnection

        strNgay = Me.tungay.Value.Day
        strThang = Me.tungay.Value.Month
        strNam = Me.tungay.Value.Year
        strTuNgay = strThang + "/" + strNgay + "/" + strNam

        strNgay = "'"
        strThang = ""
        strNam = ""
        strNgay = Me.denngay.Value.Day
        strThang = Me.denngay.Value.Month
        strNam = Me.denngay.Value.Year
        strDenNgay = strThang + "/" + strNgay + "/" + strNam

        strDonViHanhChinh = strDVHC


        If check() = True Then
            Dim parasHoSoSoDKBD() As String = {"@GiaTriMaVach", "@TuNgay", "@DenNgay", "@CapMoi"}
            Dim ValuesHoSo() As String = {Me.txtmavach.Text.ToString(), strTuNgay, strDenNgay, "1"}
            dtHoSo = cls.GetData_SP("ThongKeHoSoDuTinhPhapLy", parasHoSoSoDKBD, ValuesHoSo)
        Else
            Dim parasHoSoSoDKBD() As String = {"@GiaTriMaVach", "@TuNgay", "@DenNgay", "@CapMoi"}
            Dim ValuesHoSo() As String = {Me.txtmavach.Text.ToString(), strTuNgay, strDenNgay, "0"}
            dtHoSo = cls.GetData_SP("ThongKeHoSoDuTinhPhapLy", parasHoSoSoDKBD, ValuesHoSo)
        End If


        If dtHoSo.Rows.Count <> 0 Then
            dtHoSo.Columns.Add("Tinh")
            dtHoSo.Columns.Add("Huyen")
            dtHoSo.Columns.Add("Xa")
            dtHoSo.Rows(0).Item("Tinh") = DonViHanhChinh(0)
            dtHoSo.Rows(0).Item("Huyen") = DonViHanhChinh(1)
            dtHoSo.Rows(0).Item("Xa") = DonViHanhChinh(2)

        End If

        Dim strFileBaoCao As String = ""
        strFileBaoCao = fileReport(check)
        Dim frm As New frmBaoCao
        frm.DB = dtHoSo
        frm.FileReport = strFileBaoCao
        frm.LoadReport()
        frm.Show()
    End Sub

    Public Function fileReport(ByVal LoaiBC As Boolean) As String
        Dim ViTri As Short
        Dim path As String
        ViTri = InStr(4, Application.ExecutablePath, "\", CompareMethod.Binary)
        path = Application.ExecutablePath.Substring(0, ViTri) & Application.ProductName
        Dim file As String = ""
        If LoaiBC = True Then
            file = Application.StartupPath & "\Reports\ReportsBC\rptHoSoLapMoi.rpt"
        Else
            file = Application.StartupPath & "\Reports\ReportsBC\rptHoSoLapBienDong.rpt"
        End If

        Return file
    End Function
End Class
